/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GuiPesquisaCliente.java
 *
 * Created on 22/11/2011, 11:22:58
 */

package GUI;

import java.util.Vector;
import tp1POO.Persistencia.Pesquisa;
import tp1POO.Modelo.Cliente;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import tp1POO.GestorLog;

/**
 *
 * @author matheus
 */
public class GuiPesquisaCliente extends javax.swing.JFrame {

    /** Creates new form GuiPesquisaCliente */
    public GuiPesquisaCliente() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jComboEscolha = new javax.swing.JComboBox();
        jTInfo = new javax.swing.JTextField();
        jBPesquisa = new javax.swing.JButton();
        jBCancel = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Pesquisa Cliente");

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jComboEscolha.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CPF", "Nome" }));

        jBPesquisa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/ico_lupa.gif"))); // NOI18N
        jBPesquisa.setText("Pesquisar");
        jBPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBPesquisaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 570, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jBPesquisa)
                    .addComponent(jComboEscolha, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboEscolha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jBPesquisa)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jBCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/icoErro.gif"))); // NOI18N
        jBCancel.setText("Fechar");
        jBCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCancelActionPerformed(evt);
            }
        });

        jTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "CPF", "Endereço", "Telefone", "N. de locações"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 719, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBCancel))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jBCancel)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents




    /** Remove as linhas da tabela contendo os clientes
     * para que uma nova pesquisa seja realizada.
     */
    private void removerLinhasAntigas () {
        DefaultTableModel modelo = (DefaultTableModel)jTable.getModel();
        while (modelo.getRowCount()>0) {
            modelo.removeRow(0);
        }
        return;
    }








    /** Executado sempre que o botão "Pesquisa" é pressionado.
     * Escreve na tabela todos os clientes com o nome ou CPF especificado(dependendo da busca).
     */

    private void jBPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBPesquisaActionPerformed
        String msg = "";
        String opcao = "";
        Cliente cliente = null;
        DefaultTableModel modelo = (DefaultTableModel)jTable.getModel();

        opcao = (String)jComboEscolha.getSelectedItem();
        removerLinhasAntigas();
        if (!jTInfo.getText().isEmpty()) {
            if (opcao.compareTo("CPF") == 0) {
                cliente = Pesquisa.ObterClienteCpf(jTInfo.getText());
            }
            else if (opcao.compareTo("Nome") == 0) {
                cliente = Pesquisa.ObterClienteNome(jTInfo.getText());
            }
            if (cliente == null) {
                 JOptionPane.showMessageDialog(jPanel1,"ERRO : Cliente não existe!", "Error", JOptionPane.ERROR_MESSAGE);
            }
            else {
                msg = cliente.getNome()+"    "+cliente.getCpf()+"    "+cliente.getEndereco()+"    "+cliente.getTelefone();
                modelo.addRow( new String [] {cliente.getNome(),cliente.getCpf(),cliente.getEndereco(),cliente.getTelefone(),String.valueOf(cliente.getLocacoes().size())} );
                GestorLog.obterInstancia().inserirMsgLog(GuiCreator.obterInstancia().getGuiFuncionalidades().getFuncionario().getCpf()+
                        "Realizou uma Pesquisa de clientes");
            }
        }
        else {
                JOptionPane.showMessageDialog(jPanel1,"ERRO : Campo de texto vazio!", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jBPesquisaActionPerformed

    private void jBCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCancelActionPerformed
        GestorLog.obterInstancia().inserirMsgLog("Funcionalidade de Pesquisar clientes foi fechada("+GuiCreator.obterInstancia().getGuiFuncionalidades().getFuncionario().getCpf()+")");
        GuiCreator.obterInstancia().removerGuiPesqCliente();
    }//GEN-LAST:event_jBCancelActionPerformed























    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GuiPesquisaCliente().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBCancel;
    private javax.swing.JButton jBPesquisa;
    private javax.swing.JComboBox jComboEscolha;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTInfo;
    private javax.swing.JTable jTable;
    // End of variables declaration//GEN-END:variables

}
